<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var longestNiceSubstring = function (s) {
      for (let i = 0; i < s.length; i++) {
        const c = s[i].charCodeAt(0) < 91 ? s[i].toLowerCase() : s[i].toUpperCase();
        if (s.includes(c)) continue;
        const s1 = longestNiceSubstring(s.slice(0, i));
        const s2 = longestNiceSubstring(s.slice(i + 1));
        return s1.length >= s2.length ? s1 : s2;
      }
      return s;
    };
    console.log(longestNiceSubstring(s = "YazaAay"))
  </script>
</body>

</html>